package com.wf.day_0618.test1;

import com.wf.day_0618.test1.entity.Student;

import java.lang.reflect.Field;

/**
 * @author mpk
 * @since 2024/6/18 19:07:50
 */
/*    按要求完成下面两个方法的方法体*/
public class TestB {

    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {

        Student student = new Student("张三", 18, "北京", "");

        System.out.println(student);
        setProperty(student, "name", "李四");
        setProperty(student, "email", "123456");



        System.out.println(getProperty(student, "name"));

        System.out.println(student);
    }

    //写一个方法，此方法可将obj对象中名为propertyName的属性的值设置为value.
    public static  void setProperty(Object obj, String propertyName, Object value) throws NoSuchFieldException, IllegalAccessException {

        Field field = obj.getClass().getDeclaredField(propertyName);

        field.setAccessible(true);

        field.set(obj, value);

    }

    //写一个方法，此方法可以获取obj对象中名为propertyName的属性的值
    public static Object getProperty(Object obj, String propertyName) throws NoSuchFieldException, IllegalAccessException {

        Field field = obj.getClass().getDeclaredField(propertyName);

        field.setAccessible(true);


        return   field.get(obj);
    }

}
